package grex.Nodes.Depricated;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
import grex.BNFException;
import grex.ErrorManagement.ErrorManager;
import grex.Data.Prediction;
import grex.Data.PredictionContainer;
import grex.Nodes.GeneException;
import grex.Nodes.IContinuousOperator;
import grex.Nodes.ITerminal;
import grex.Nodes.Node;

public class ProbVar extends Node implements ITerminal {

    private static final int NROFCILDREN = 0;
    private int conVarNr = -1;
    private int catVarNr = -1;
    private int varNr=-1;
    private int count=0;
    public ProbVar() throws BNFException {
        super(NROFCILDREN);
    }

    public synchronized void execute(Node parent, PredictionContainer data, int mode) throws GeneException {
        this.parent = parent;
        if(mode==Node.TRAIN_MODE){
            count=data.values().size();
        }
        init();
        for (Prediction p : data.values()) {
            p.setPrediction(p.getInstance()[varNr]);
            p.setLeaf(this);
            p.setProb(new Double(0));
        }
    }

    public void setVarNr(int varNr) {
        this.varNr = varNr;
    }

    public int getVarNr() {
        return varNr;
    }

    public Object clone() {
        ProbVar newVariable;
        try {
            newVariable = new ProbVar();
            newVariable.setVarNr(varNr);
            newVariable.setOptions(ops);
            newVariable.setEnvironment(environment);
            newVariable.setParent(parent);
            newVariable.catVarNr=catVarNr;
            newVariable.conVarNr=conVarNr;
        } catch (grex.BNFException e) {
            ErrorManager.getInstance().reportError(e);
            return null;
        }
        return newVariable;
    }

    public String toString() {

            if (varNr == -1) {
                return "Unused";
            }
            return environment.getVariableNames()[varNr];
    }

    public double getProb() {
        this.getType();
        return 0;
    }

    public double getLowProb() {
        return 0;
    }

    public int getCount() {
        return count;
    }

    private void init() throws GeneException {
        if (parent instanceof IContinuousOperator) {
            if (conVarNr == -1) {
                conVarNr = environment.getRandomProbContiousColumnIndex();
            }
            varNr = conVarNr;
        } else {
            //Categorisk operator
            if (catVarNr == -1) {
                catVarNr = environment.getRandomCategoricalColumnIndex();
            }
            varNr = catVarNr;
        }
    }
}
